package com.example.myapplication.geofence

import android.Manifest
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.util.Log
import androidx.core.app.ActivityCompat
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingClient
import com.google.android.gms.location.GeofencingRequest
import com.google.android.gms.location.LocationServices
import com.google.android.gms.tasks.Task

/**
 * Author: Jaydenyan
 * Date: 2022/5/17 11:11
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * Jaydenyan 2022/5/17 1.0 首次创建
 */
class GeofenceManager private constructor() {
    private val TAG = GeofenceManager::class.java.simpleName

    companion object {
        public const val GEOFENCE_TRANSITION_ENTER = Geofence.GEOFENCE_TRANSITION_ENTER
        public const val GEOFENCE_TRANSITION_EXIT = Geofence.GEOFENCE_TRANSITION_EXIT
        public const val GEOFENCE_TRANSITION_DWELL = Geofence.GEOFENCE_TRANSITION_DWELL
        fun newInstance() = GeofenceManager()
    }

    private var geofencingClient: GeofencingClient? = null
    public fun initGeofencingClient(context: Context): GeofenceManager {
        geofencingClient = LocationServices.getGeofencingClient(context)
        return this
    }

    /**
     * //https://developers.google.cn/android/reference/com/google/android/gms/location/Geofence.Builder?hl=en
     */
    public fun getGeofencing(
        latitude: Double,
        longitude: Double,
        radius: Float,
        key: String,
        transitionTypes: Int,
        mLoiteringDelay: Int? = null
    ): Geofence {
        return Geofence.Builder()
            .setRequestId(key)
            .setCircularRegion(
                latitude,
                longitude,
                radius
            )
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setTransitionTypes(transitionTypes)
            .setLoiteringDelay(mLoiteringDelay ?: 5000)
            .build()

    }

    private fun getGeofencingRequest(geofenceList: ArrayList<Geofence>): GeofencingRequest {
        return GeofencingRequest.Builder().apply {
            setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER or GeofencingRequest.INITIAL_TRIGGER_EXIT or GeofencingRequest.INITIAL_TRIGGER_DWELL)
            addGeofences(geofenceList)
        }.build()
    }

    private fun getGeofencePendingIntent(context: Context): PendingIntent {
        return PendingIntent.getBroadcast(
            context,
            0,
            Intent(context, GeofenceBroadcastReceiver::class.java),
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
        )
    }

    @SuppressLint("MissingPermission")
    public fun startGeofencing(context: Context, geofenceList: ArrayList<Geofence>): Task<Void>? {
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return null
        }
        var req = getGeofencingRequest(geofenceList)
        return geofencingClient?.addGeofences(req, getGeofencePendingIntent(context))
    }

    public fun startGeofencing(context: Context, mGeofence: Geofence): Task<Void>? {
        return startGeofencing(context, arrayListOf(mGeofence))
    }

    public fun stopAllGeofencing(context: Context): Task<Void>? {
        return geofencingClient?.removeGeofences(getGeofencePendingIntent(context))
    }

    public fun stopGeofencing(key: String): Task<Void>? {
        return geofencingClient?.removeGeofences(arrayListOf(key))

    }


}